package UDP_chat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class ChatClientUDP {
	public static void main(String[] args) throws IOException{
		DatagramSocket clientSK;
		DatagramPacket clientPK;
		String messenger;
		do
		{	//1. tao socket
			clientSK=new DatagramSocket();
			Scanner input=new Scanner(System.in);//doc noi dung
			System.out.println("From Client: ");
			messenger=input.nextLine();
			clientPK=new DatagramPacket(messenger.getBytes(), messenger.getBytes().length, InetAddress.getLocalHost(), 7777);
			
			clientSK.send(clientPK);
			byte []buf=new byte[1024];
			clientPK=new DatagramPacket(buf, buf.length);
			clientSK.receive(clientPK);
			messenger=new String(clientPK.getData());
			System.out.println("Server:"+messenger.trim());
			input.close();
		}
		while(!messenger.trim().equalsIgnoreCase("exit"));
		clientSK.close();
		
	}
}
